package com.example.net.websocket.service;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.IBinder;
import android.util.Log;

import androidx.core.app.NotificationCompat;

import com.example.net.R;
import com.example.net.websocket.WSManager;
import com.example.net.websocket.WSThread;

/**
 * 启动websocket
 * 启动采集信息线程
 * 启动WsThread来发送websocket
 */
public class WebSocketService extends Service {
    private static final String TAG = "WebSocketService";
    private static boolean isRunning = false;
    private WSThread wsThread;
    private NotificationManager manager;
    public static void startWebSocket(Context context) {
       Intent socketService = new Intent(context, WebSocketService.class);
        //启动一个前台服务 在api大于26才可使用startForegroundService此方法
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            context.startForegroundService(socketService);
        }else{
            context.startService(socketService);
        }
        Log.i(TAG, "startWebSocket: ");

    }
    public static void stopWebSocket(Context context) {
        if(!isRunning)
            return;
        Log.i(TAG, "stopWebSocket: ");
        context.stopService(new Intent(context, WebSocketService.class));
    }


    public WebSocketService() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Log.i(TAG, "onCreate: ");
        Log.i(TAG, "================开启WebSocketService================ ");
        if(isRunning)
            return;
        isRunning = true;
        manager= (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    }
    private void startWsManager(){
        Log.i(TAG, "startWsManager: ");
        boolean isOk = WSManager.getInstance().init();
        //如果初始化成功，则开始进行发送信息
        if (isOk) {
            Log.i(TAG, "================WebSocket初始化成功，则开始进行发送信息================ ");
            wsThread = new WSThread();
            wsThread.startThread();
        }
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.i(TAG, "onStartCommand: ");
        // If the notification supports a direct reply action, use
            // PendingIntent.FLAG_MUTABLE instead.
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            Log.i(TAG, "Foreground onStartCommand: ");
            NotificationChannel notificationChannel = new NotificationChannel("channel_1011","ai服务",NotificationManager.IMPORTANCE_HIGH);
            //如果这里用IMPORTANCE_NOENE就需要在系统的设置里面开启渠道，通知才能正常弹出
            manager.createNotificationChannel(notificationChannel);
            // 创建Notification通知
            Notification notification = new NotificationCompat.Builder(this,"channel_1011")
                    .setContentTitle("Ai Service")
                    .setContentText("Running...")
                    .setSmallIcon(com.example.baseutils.R.drawable.xiaonuo)
                    .setPriority(NotificationCompat.PRIORITY_DEFAULT)
                    .build();
            startForeground(1, notification);
        }
        startWsManager();
        return super.onStartCommand(intent, flags, startId);

    }

    public static boolean isServiceRunning() {
        return isRunning;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.i(TAG, "================关闭WebSocketService================ ");
        isRunning = false;

        WSManager.getInstance().disconnect();
        wsThread.stopThread();
    }
}
